package net.huashitong.ssydt.provider.pk.service;


import net.huashitong.ssydt.pk.constant.PkConsts;
import net.huashitong.ssydt.pk.entity.*;
import net.huashitong.ssydt.pk.entity.vo.PkInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.12.22
 */
@Service
@Transactional
public class PkServiceImpl implements PkService {

    private SsPkYmdxMatchRecordService ssPkYmdxMatchRecordService;

    private SsPkYmdxMyRankService ssPkYmdxMyRankService;

    private SsPkYmdxMyMedalService ssPkYmdxMyMedalService;

    private SsPkYzddMatchRecordService ssPkYzddMatchRecordService;

    private SsPkYzddMyRankService ssPkYzddMyRankService;

    private SsPkYzddMyMedalService ssPkYzddMyMedalService;

    private SsPkGameRuleService ssPkGameRuleService;


    @Autowired
    public PkServiceImpl(SsPkYmdxMatchRecordService ssPkYmdxMatchRecordService,
                         SsPkYmdxMyRankService ssPkYmdxMyRankService,
                         SsPkYmdxMyMedalService ssPkYmdxMyMedalService,
                         SsPkYzddMatchRecordService ssPkYzddMatchRecordService,
                         SsPkYzddMyRankService ssPkYzddMyRankService,
                         SsPkYzddMyMedalService ssPkYzddMyMedalService,
                         SsPkGameRuleService ssPkGameRuleService) {
        this.ssPkYmdxMatchRecordService = ssPkYmdxMatchRecordService;
        this.ssPkYmdxMyRankService = ssPkYmdxMyRankService;
        this.ssPkYmdxMyMedalService = ssPkYmdxMyMedalService;
        this.ssPkYzddMatchRecordService = ssPkYzddMatchRecordService;
        this.ssPkYzddMyRankService = ssPkYzddMyRankService;
        this.ssPkYzddMyMedalService = ssPkYzddMyMedalService;
        this.ssPkGameRuleService = ssPkGameRuleService;
    }

    @Override
    public PkInfoVO getInfo(String userId) {
        PkInfoVO vo = new PkInfoVO();
        PkInfoVO.InfoVO yzddInfoVO = getYzddInfoVO(userId);
        PkInfoVO.InfoVO ymdxInfoVO = getYmdxInfoVO(userId);
        vo.setYzdd(yzddInfoVO);
        vo.setYmdx(ymdxInfoVO);
        return vo;
    }

    @Override
    public int updateThis(SsPkGameRule record) {
        int i = ssPkGameRuleService.updateByPrimaryKeySelective(record);
        if (i != 0) {
            if (record.getType().equals(PkConsts.RuleType.YMDX)) {
                SsPkYmdxMatchRecord ssPkYmdxMatchRecord = ssPkYmdxMatchRecordService.getNowYmdxMatch();
                ssPkYmdxMatchRecord.setFreeTimes(record.getFreeTimes());
                ssPkYmdxMatchRecord.setFirstGold(record.getFirstGold());
                ssPkYmdxMatchRecord.setSecondGold(record.getSecondGold());
                ssPkYmdxMatchRecord.setThirdGold(record.getThirdGold());
                ssPkYmdxMatchRecord.setFourthToTenthGold(record.getFourthToTenthGold());
                ssPkYmdxMatchRecord.setRemoveErrorGold(record.getRemoveErrorGold());
                ssPkYmdxMatchRecord.setSelectedCorrectGold(record.getSelectedCorrectGold());
                ssPkYmdxMatchRecord.setRightAnswerGold(record.getRightAnswerGold());
                ssPkYmdxMatchRecord.setChallengeGold(record.getChallengeGold());
                ssPkYmdxMatchRecordService.updateByPrimaryKey(ssPkYmdxMatchRecord);
            } else if (record.getType().equals(PkConsts.RuleType.YZDD)) {
                SsPkYzddMatchRecord ssPkYzddMatchRecord = ssPkYzddMatchRecordService.getNowYzddMatch();
                ssPkYzddMatchRecord.setFreeTimes(record.getFreeTimes());
                ssPkYzddMatchRecord.setFirstGold(record.getFirstGold());
                ssPkYzddMatchRecord.setSecondGold(record.getSecondGold());
                ssPkYzddMatchRecord.setThirdGold(record.getThirdGold());
                ssPkYzddMatchRecord.setFourthToTenthGold(record.getFourthToTenthGold());
                ssPkYzddMatchRecord.setRemoveErrorGold(record.getRemoveErrorGold());
                ssPkYzddMatchRecord.setSelectedCorrectGold(record.getSelectedCorrectGold());
                ssPkYzddMatchRecord.setRightAnswerGold(record.getRightAnswerGold());
                ssPkYzddMatchRecord.setChallengeGold(record.getChallengeGold());
                ssPkYzddMatchRecord.setReviveGold(record.getReviveGold());
                ssPkYzddMatchRecordService.updateByPrimaryKey(ssPkYzddMatchRecord);
            }
        }
        return 1;
    }

    private PkInfoVO.InfoVO getYmdxInfoVO(String userId) {
        SsPkYmdxMatchRecord matchRecord = ssPkYmdxMatchRecordService.getNowYmdxMatch();
        SsPkYmdxMyRank ymdxMyRank = ssPkYmdxMyRankService.getAutoUpdateRankByMatchRecordIdWithUserId(matchRecord.getId(), userId);
        SsPkYmdxMyMedal ymdxMedal = ssPkYmdxMyMedalService.getAutoCreateByUserId(userId);

        PkInfoVO.InfoVO vo = new PkInfoVO.InfoVO();
        vo.setRanking(ymdxMyRank.getRanking());
        vo.setScore(ymdxMyRank.getHighestScore());
        vo.setGoldMedal(ymdxMedal.getGoldMedal());
        vo.setSilverMedal(ymdxMedal.getSilverMedal());
        vo.setBronzeMedal(ymdxMedal.getBronzeMedal());
        return vo;
    }

    private PkInfoVO.InfoVO getYzddInfoVO(String userId) {
        SsPkYzddMatchRecord matchRecord = ssPkYzddMatchRecordService.getNowYzddMatch();
        SsPkYzddMyRank yzddMyRank = ssPkYzddMyRankService.getAutoUpdateRankByMatchRecordIdWithUserId(matchRecord.getId(), userId);
        SsPkYzddMyMedal yzddMedal = ssPkYzddMyMedalService.getAutoCreateByUserId(userId);

        PkInfoVO.InfoVO vo = new PkInfoVO.InfoVO();
        vo.setRanking(yzddMyRank.getRanking());
        vo.setScore(yzddMyRank.getHighestScore());
        vo.setGoldMedal(yzddMedal.getGoldMedal());
        vo.setSilverMedal(yzddMedal.getSilverMedal());
        vo.setBronzeMedal(yzddMedal.getBronzeMedal());
        return vo;
    }
}
